/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EventoModificar.java
 *
 * Created on May 31, 2012, 5:20:37 PM
 */
package sistemadeproyectos;

import java.sql.*;
import javax.swing.JOptionPane;

public class EventoModificar extends javax.swing.JDialog {

  static String fecha;
  static String equipo;
    /** Creates new form EventoModificar */
   public EventoModificar(String fecha, String equipo) {
     this.fecha = fecha;
     this.equipo = equipo;  
        
  BDAdmin modif = new BDAdmin("SELECT * FROM CALENDARIO");
  
  ResultSet rs = modif.consulta();
  String date;
  boolean x = true;
  boolean y = false;
  
  try {
   if (rs.next()){
     while (x){
      date = rs.getString("FECHA");
       if (fecha.equals(date)){
        if(this.equipo.equals(rs.getString("EQUIPO"))) {
         initComponents();   
         y = true; 
        }
        else { //Se verifica que la actividad a modificar sea del mismo equipo
          JOptionPane.showOptionDialog(
          this,
          "No puede actualizar actividades de otro equipo.", //Mensaje
          "",
          JOptionPane.OK_OPTION,
          JOptionPane.NO_OPTION,
          null,
          new Object[] { "Ok"}, 
          "");
         }
       }
       x = rs.next();
     }
     if (y==false) {
      JOptionPane.showOptionDialog(
      this,
      "No hay actividades pendientes para este dia", //Mensaje
       "",
       JOptionPane.OK_OPTION,
       JOptionPane.NO_OPTION,
       null,
       new Object[] { "Ok"}, 
      "");
         }
        }
       }
       catch (Exception e) {
       e.printStackTrace();
       }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jTextField1 = new javax.swing.JTextField();
        jToggleButton1 = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sistemadeproyectos.SistemadeproyectosApp.class).getContext().getResourceMap(EventoModificar.class);
        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jToggleButton1.setText(resourceMap.getString("jToggleButton1.text")); // NOI18N
        jToggleButton1.setName("jToggleButton1"); // NOI18N
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButtonOk(evt);
            }
        });

        jToggleButton2.setText(resourceMap.getString("jToggleButton2.text")); // NOI18N
        jToggleButton2.setName("jToggleButton2"); // NOI18N
        jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButtonCancelar(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(179, 179, 179)
                .addComponent(jLabel1)
                .addContainerGap(238, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                            .addGap(52, 52, 52)
                            .addComponent(jToggleButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(49, 49, 49)
                            .addComponent(jToggleButton2)
                            .addGap(55, 55, 55))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 414, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(47, Short.MAX_VALUE)))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jToggleButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToggleButton1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jToggleButtonOk(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButtonOk
  
  BDAdmin modif = new BDAdmin("SELECT * FROM CALENDARIO");
  
  ResultSet rs = modif.consulta();
  String actividad;
  String hora;
  String date;
  char dosPuntos1,dosPuntos2;
  char [] dst = new char[6];
  boolean x = true;
  boolean y = false;
  
  try {	
   if (rs.next()){
     while (x){
      date = rs.getString("FECHA");
       if (fecha.equals(date)){
        if(this.equipo.equals(rs.getString("EQUIPO"))) {
         y = true; 
         actividad = jTextArea1.getText();
         hora = jTextField1.getText();
         dst[0]=hora.charAt(0);
         dst[1]=hora.charAt(1);
         dst[2]=hora.charAt(3);
         dst[3]=hora.charAt(4);
         dst[4]=hora.charAt(6);
         dst[5]=hora.charAt(7);
         dosPuntos1 = hora.charAt(2);
         dosPuntos2 = hora.charAt(5);
    if(dosPuntos1 != ':' || dosPuntos2 != ':'){
        JOptionPane.showOptionDialog(
          this,
          "El formato de hora debe ser hh:mm:ss", //Mensaje
          "",
          JOptionPane.OK_OPTION,
          JOptionPane.NO_OPTION,
          null,
          new Object[] { "Ok"}, 
          "");
  }
  
  if(dst[0]!='0' && dst[0]!='1' && dst[0]!='2'){
     JOptionPane.showOptionDialog(
          this,
          "El formato de hora debe ser [0-2][0-3]:[0-5][0-9]:[0-5][0-9]", //Mensaje
          "",
          JOptionPane.OK_OPTION,
          JOptionPane.NO_OPTION,
          null,
          new Object[] { "Ok"}, 
          "");
  }
  if(dst[1]!='0' && dst[1]!='1' && dst[1]!='2' && dst[1]!='3'){
     JOptionPane.showOptionDialog(
          this,
          "El formato de hora debe ser [0-2][0-3]:[0-5][0-9]:[0-5][0-9]", //Mensaje
          "",
          JOptionPane.OK_OPTION,
          JOptionPane.NO_OPTION,
          null,
          new Object[] { "Ok"}, 
          "");
  }
  if(dst[2]!='0' && dst[2]!='1' && dst[2]!='2' && dst[2]!='3' && dst[2]!='4' && dst[2]!='5'){
     JOptionPane.showOptionDialog(
          this,
          "El formato de hora debe ser [0-2][0-3]:[0-5][0-9]:[0-5][0-9]", //Mensaje
          "",
          JOptionPane.OK_OPTION,
          JOptionPane.NO_OPTION,
          null,
          new Object[] { "Ok"}, 
          "");
  }
  if(dst[3]!='0' && dst[3]!='1' && dst[3]!='2' && dst[3]!='3' && dst[3]!='4' && dst[3]!='5' && dst[3]!='6' && dst[3]!='7' && dst[3]!='8' && dst[3]!='9'){
     JOptionPane.showOptionDialog(
          this,
          "El formato de hora debe ser [0-2][0-3]:[0-5][0-9]:[0-5][0-9]", //Mensaje
          "",
          JOptionPane.OK_OPTION,
          JOptionPane.NO_OPTION,
          null,
          new Object[] { "Ok"}, 
          "");
  }
  if(dst[4]!='0' && dst[4]!='1' && dst[4]!='2' && dst[4]!='3' && dst[4]!='4' && dst[4]!='5'){
     JOptionPane.showOptionDialog(
          this,
          "El formato de hora debe ser [0-2][0-3]:[0-5][0-9]:[0-5][0-9]", //Mensaje
          "",
          JOptionPane.OK_OPTION,
          JOptionPane.NO_OPTION,
          null,
          new Object[] { "Ok"}, 
          "");
  }
  if(dst[5]!='0' && dst[5]!='1' && dst[5]!='2' && dst[5]!='3' && dst[5]!='4' && dst[5]!='5' && dst[5]!='6' && dst[5]!='7' && dst[5]!='8' && dst[5]!='9'){
     JOptionPane.showOptionDialog(
          this,
          "El formato de hora debe ser [0-2][0-3]:[0-5][0-9]:[0-5][0-9]", //Mensaje
          "",
          JOptionPane.OK_OPTION,
          JOptionPane.NO_OPTION,
          null,
          new Object[] { "Ok"}, 
          "");
  }
         modif.setQuery("UPDATE CALENDARIO SET ACTIVIDADES = '" +actividad + "', FECHA ='" + fecha + "', HORA = '" + hora + "', EQUIPO = '" + this.equipo + "' WHERE FECHA = '"+fecha+"'");
         modif.actualizar(); 
         this.setVisible(false);
        }
       }
       x = rs.next();
      }
      modif.cerrar(); 
     }
    }
       catch (Exception e) {
       e.printStackTrace();
       }
  
}//GEN-LAST:event_jToggleButtonOk

private void jToggleButtonCancelar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButtonCancelar
  this.setVisible(false);
}//GEN-LAST:event_jToggleButtonCancelar

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EventoModificar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EventoModificar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EventoModificar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EventoModificar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                EventoModificar dialog = new EventoModificar(fecha, equipo);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton2;
    // End of variables declaration//GEN-END:variables
}
